package br.com.fogas.utils.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;

public class DialogAlert extends DialogFragment {
	
	private Integer title;
	
	private Integer message;
	
	private Integer icon;
	
	private CharSequence charMessage;
	
	private DialogInterface.OnClickListener onPositiveClick;
	
	public static DialogAlert newInstance() {
		return new DialogAlert();
	}
	
	public DialogAlert setTitle(Integer title) {
		this.title = title;
		return this;
	}
	
	public DialogAlert setMessage(Integer message) {
		this.message = message;
		return this;
	}
	
	public DialogAlert setCharMessage(CharSequence charMessage) {
		this.charMessage = charMessage;
		return this;
	}
	
	public DialogAlert setPositiveClick(DialogInterface.OnClickListener onPositiveClick) {
		this.onPositiveClick = onPositiveClick;
		return this;
	}
	
	public DialogAlert setIcon(Integer icon) {
		this.icon = icon;
		return this;
	}
	
	public DialogAlert showDialog(FragmentManager fragmentManager) {
		this.show(fragmentManager, "DIALOG_ALERT");
		return this;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
		
		if(title != null) {
			dialog.setTitle(title);
		}
		
		if(onPositiveClick != null) {
			dialog.setPositiveButton(android.R.string.ok, onPositiveClick);
		} else {
			dialog.setPositiveButton(android.R.string.ok, onPositiveClickDefault);
		}

		if (charMessage != null) {
			dialog.setMessage(charMessage);
		} else {
			dialog.setMessage(message);
		}
		
		if(icon != null) {
			dialog.setIcon(icon);
		}
		
		return dialog.create();
	}
	
	DialogInterface.OnClickListener onPositiveClickDefault = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}
	};
}